/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppMarketingPlanSaveParam;
import com.jnzc.ocr.serv.shop.api.ShopMarketingPlanApi;
import com.jnzc.ocr.serv.shop.entity.ShopMarketingPlan;
import com.jnzc.ocr.serv.shop.filter.ShopMarketingPlanFilter;
import com.jnzc.ocr.serv.shop.vo.ShopMarketingPlanSelectResultVo;
import com.jnzc.ocr.serv.shop.vo.ShopMarketingPlanSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Api(tags = {"app - 营销方案接口"})
@RestController
@RequestMapping("/app/marketingPlans")
public class AppMarketingPlanController extends BaseController {

    @Autowired
    private ShopMarketingPlanApi shopMarketingPlanApi;

    @ApiOperation(value = "分页")
    @GetMapping("")
    public PageResult<ShopMarketingPlan> search(Page page){
        UserVo userVo = super.get();
        ShopMarketingPlanFilter marketingPlanFilter = new ShopMarketingPlanFilter();
        marketingPlanFilter.setShopId(userVo.getShopId());
        return shopMarketingPlanApi.search(marketingPlanFilter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopMarketingPlan info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopMarketingPlanApi.find(id, userVo.getShopId());
    }

    @ApiOperation(value = "保存")
    @PostMapping("")
    public void save(@RequestBody AppMarketingPlanSaveParam param){
        UserVo userVo = super.get();
        ShopMarketingPlan obj;
        if(null == param.getId()){
            obj = new ShopMarketingPlan();
            obj.setShopId(userVo.getShopId());
        } else {
            obj = shopMarketingPlanApi.find(param.getId(), userVo.getShopId());
        }
        obj.setName(param.getName());
        obj.setMarketingType(param.getMarketingType());
        obj.setMarketingTypeName(null);
        obj.setProductCategoryId(param.getProductCategoryId());
        obj.setProductCategoryName(null);
        obj.setMemberCategoryId(param.getMemberCategoryId());
        obj.setMemberCategoryName(null);
        obj.setStartTerm(param.getStartTerm());
        obj.setEndTerm(param.getEndTerm());
        shopMarketingPlanApi.save(obj);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        UserVo userVo = super.get();
        shopMarketingPlanApi.delete(id, userVo.getShopId());
    }

    @ApiOperation(value = "选择营销方案")
    @PostMapping("/selectPlan")
    public Map<String, ShopMarketingPlanSelectResultVo> selectPlan(@RequestBody ShopMarketingPlanSelectVo selectVo){
        UserVo userVo = super.get();
        return shopMarketingPlanApi.selectPlan(selectVo, userVo.getShopId());
    }
}
